/******************************************************************
 * @version      : 1.0
 * @Author       : zhl
 * @Date         : 2023-06-26 14:18:52
 * @LastEditors  : zhl
 * @E-Mail       : zhll168@163.com
 * @Description  : 业务描述
 ******************************************************************/
#include "CONNECT-Request.h"
#include "DLT698-Frame.h"

ConnectRequest ConnectRequest::c_connect_request;

ConnectRequest::ConnectRequest(/* args */){
}

ConnectRequest::~ConnectRequest(){
}

ConnectRequest& ConnectRequest::getInstance(){
    return c_connect_request;
}

int ConnectRequest::packet(uint8_t* data, int& len, CONNECT_REQUEST_FRAME reqFrame, int start){
    if((NULL== data) || (start > len) || (1 > len)){
        return -1;
    }
    int offset = 0;
    data[start] = DLT698Frame::getInstance().get_piid();
    offset += 1;
    data[start+offset] = reqFrame.protocol_ver;
    offset += sizeof(reqFrame.protocol_ver);
    memcpy(data+start+offset, reqFrame.protocol_conformance.value, sizeof(reqFrame.protocol_conformance.value));
    offset += sizeof(reqFrame.protocol_conformance.value);
    memcpy(data+start+offset, reqFrame.function_conformance.value, sizeof(reqFrame.function_conformance.value));
    offset += sizeof(reqFrame.function_conformance.value);
    data[start+offset] = reqFrame.frame_max_size;
    offset += sizeof(reqFrame.frame_max_size);
    data[start+offset] = reqFrame.recv_max_size;
    offset += sizeof(reqFrame.recv_max_size);
    data[start+offset] = reqFrame.recv_max_window_size;
    offset += sizeof(reqFrame.recv_max_window_size);
    data[start+offset] = reqFrame.handle_fame_max_size;
    offset += sizeof(reqFrame.handle_fame_max_size);
    data[start+offset] = reqFrame.expected_timeout;
    offset += sizeof(reqFrame.expected_timeout);
    data[start+offset] = reqFrame.connect_mechanism.tag;
    offset += sizeof(reqFrame.connect_mechanism.tag);
    //缺少内容,长度不对
    switch(reqFrame.connect_mechanism.tag){
        case CONNECT_MECHANISM_NULL_SECURITY:
        break;
        case CONNECT_MECHANISM_PWD_SECURITY:
            memcpy(data+start+offset, reqFrame.connect_mechanism.cipher, sizeof(reqFrame.connect_mechanism.cipher));
            offset += sizeof(reqFrame.connect_mechanism.cipher);
        break;
        case CONNECT_MECHANISM_SYMMETRY_SECURITY:
        case CONNECT_MECHANISM_SIGNATURE_SECURITY:
            memcpy(data+start+offset, reqFrame.connect_mechanism.cipher, sizeof(reqFrame.connect_mechanism.cipher));
            offset += sizeof(reqFrame.connect_mechanism.cipher);
            memcpy(data+start+offset, reqFrame.connect_mechanism.signature, sizeof(reqFrame.connect_mechanism.signature));
            offset += sizeof(reqFrame.connect_mechanism.signature);
        break;
        default:
        break;
    }

    return offset;
}

int ConnectRequest::unpack(uint8_t* data, int& len, int start){

}

CONNECT_REQUEST_FRAME& ConnectRequest::get_connect_req_params(){
    return c_connect_req_params;
}